import theme from '@/uni.scss'
import {
	THEME_CONFIG
} from '@/config/cachekey';
import Cache from '@/utils/cache'
import {
	apiCartNum
} from '@/api/store'
import {
	apiGetCartList,
	apiDeleteCartGoods,
	apiCartCountChange
} from '@/api/shopping'
const state = {
	cartNum: 0,
	shopList: [],
};

const mutations = {
	setCartNum(state, data) {
		state.cartNum = data
	},
	setShopList(state, data) {
		state.shopList = data
		state.cartNum = data.length
	},
};

const actions = {
	getCartNum({
		state,
		commit,
		getters
	}) {
		// 未登录
		if (!getters.isLogin) {
			return
		}
		apiCartNum().then(res => {
			commit('setCartNum', res.num)
		}).catch(() => {
			commit('setCartNum', 0)
		})
	},
	// 获取购物车列表
	getCartList({
		state,
		commit,
		getters
	}) {
		console.log('获取购物车列表')
		apiGetCartList({}).then(res => {
			console.log('res', res)
			commit('setShopList', res)
		})
	},
	// 修改购物车商品数量
	cartCountChange({
		state,
		commit,
		getters
	}, {id, num}) {

		apiCartCountChange({
			shop_param: {
				id: id,
				num: num
			}
		}).then(res => {
			const list = res.cart_list.slice()
			commit('setShopList', list)
		})
	},
	// 删除购物车商品
	delShop({
		state,
		commit,
		getters
	}, id) {
		console.log(id)
		apiDeleteCartGoods({
			shop_param: {
				id: id
			}
		}).then(res => {
			commit('setShopList', res.cart_list)
		})
	}
};

export default {
	state,
	mutations,
	actions
};
